import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:videotop/base_model.dart';
import 'package:videotop/myinfo.dart';
import 'package:videotop/util.dart';
import 'package:path_provider/path_provider.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

/// created by hx
///    2021/1/27:10:22

class ReportModel extends BaseModel{

  int uid;
  MyInfo myInfo;
  Util util;
  BuildContext context;
  ReportModel(this.context,this.uid,this.myInfo,this.util);

  List<String> list = [
    'Pornographic harassment',
    'Bloody violence',
    'Provide pornographic services',
    'Spam',
    'Fraud(wine tray...)',
    'Violation of national law',
    'Political elements',
    'Other',
  ];

  String groupValue = '-1';

  List<File> listBean = [];

  String img='';
  String miaosu = '';

  addImg() async {
    List<AssetEntity> assets = await AssetPicker.pickAssets(
      context,
      requestType: RequestType.image,
      maxAssets: 3,
      textDelegate: EnglishTextDelegate(),
      gridCount: 4,
      routeCurve: Curves.easeIn,
      routeDuration: Duration(milliseconds: 500),
    );
    if(assets != null){
      if(listBean.length<3){
        for(int i = 0;i< assets.length ;i++){
          if(listBean.length + 1 < 4){
            listBean.add(await assets[i].file);
          }else{
            util.showToast('Choose up to 3 pictures');
            break;
          }
        }
        notifyListeners();
      }else{
        util.showToast('Choose up to 3 pictures');
      }
    }
  }

  getReport() async{
    util.waiting(context);

    FocusScope.of(context).requestFocus();
    if( groupValue != '-1'){
      img = '[';
      for(int i = 0; i< listBean.length;i++){
        upimg(listBean[i], i);
      }
      String imgs = img.substring(0, img.length - 1) + ']';
      util.getreportUser(uid, int.parse(groupValue), imgs, miaosu,myInfo.uid).then((value){
        Navigator.pop(context);

        if(value == 1){
          util.showToast('Report successful');
          Navigator.pop(context);
        }else{
          util.showToast('Report failed');
        }
      });
    }else{
      Navigator.pop(context);
      util.showToast('Please fill in the complete information');
    }
  }

  upimg(File file,int i) async{
    Directory appDocDir = await getApplicationDocumentsDirectory();
    String dir = appDocDir.path;
    final targetPath = new File('$dir/ddd$i.jpg').path;
    var result = await FlutterImageCompress.compressAndGetFile(
      file.path,
      targetPath,
      quality: 80,
    );
    String name = DateTime.now().millisecondsSinceEpoch.toString() +
        '_' +
        (new Random().nextInt(10000) + 10000).toString() +
        '.jpg';
    img = img + '\"' + util.upurl_video +'image/'+ name + '\",';
    int ok = await util.upimg(
      '${util.appneame}/image/' + name,
      result.path,
    );
  }

  choose(String value){
    groupValue = value;
    notifyListeners();
  }

  removeAt(int index){
    listBean.removeAt(index);
    notifyListeners();
  }
}